﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using HK2009.BusinessFacade;
using HK2009.EntityManage;
using HK2009.Common;
using HK2009.EntityQuery;
using System.Data;
namespace HK2009.BusinessFacadeHandler
{
    public class SyClientHandler:ISyClientHandler
    {
        Sy_ClientManage clientManage = new Sy_ClientManage();
        Sy_ClientLinkManManage linkManManage = new Sy_ClientLinkManManage();
        Sy_CodeManage codeManage = new Sy_CodeManage();
        DataBase db = new DataBase();
        Sy_ClientTypeManage typeManage = new Sy_ClientTypeManage();
        /// <summary>
        /// 根据客户主键ID获取客户信息
        /// </summary>
        /// <param name="pkID"></param>
        /// <returns></returns>
        public Sy_ClientQuery GetClient(int pkID)
        {
            return clientManage.GetSy_ClientQuery(pkID);
        }

        /// <summary>
        /// 获取所有客户类别
        /// </summary>
        /// <returns></returns>
        public DataTable GetClientTypeList()
        {
            SqlParameter[] parms = new SqlParameter[2];
            parms[0] = new SqlParameter("@Status", "1");
            parms[1] = new SqlParameter("@Type","CT");
           return codeManage.GetSy_CodeList(" Status=@Status and Type=@Type", parms);

        }


        /// <summary>
        /// 根据客户代码返回客户列表
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public DataTable GetClientListByCode(string code)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@Code",code);
            return codeManage.GetSy_CodeList(" Code=@Code",parms);
        }

        /// <summary>
        /// 根据客户类型ＩＤ获取客户列表
        /// </summary>
        /// <param name="typeID"></param>
        /// <returns></returns>
        public DataTable GetClientListByTypeID(int typeID)
        {
            SqlParameter[] parms = new SqlParameter[2];
            parms[0] = new SqlParameter("@TypeID",typeID.ToString());
            parms[1] = new SqlParameter("@Status","1");
            string sqlStr = "select * from Sy_Client where PKID in(select ClientID from Sy_ClientType where SyCodeID=@TypeID) and Status=@Status";
            return db.RunSqlTab(sqlStr, parms);
        }

        /// <summary>
        /// 根据sql语句同参数插入客户相关表
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public bool ClientEdit(string sqlStr, SqlParameter[] parms)
        {
          return  db.ExecSqlTransaction(sqlStr, parms);
        }

        /// <summary>
        /// 根据客户ＩＤ获取客户所属类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetClientTypeByID(int id)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@ClientID",id.ToString());
            return typeManage.GetSy_ClientTypeList(" ClientID=@ClientID", parms);
        }

        /// <summary>
        /// 根据sql语句同参数获取客户列表
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public DataTable GetClientList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);        
        }

        /// <summary>
        /// 根据客户ID获取客户信息把客户删除(状态修改为2）
        /// </summary>
        /// <returns></returns>
        public bool ClientDelete(Sy_ClientQuery query)
        {
            return clientManage.UpdateSy_Client(query);
        }

        /// <summary>
        /// 根据sql语句同参数获取客户联系人列表
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public DataTable GetClientLinkManList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);
        }

        /// <summary>
        /// 根据id获取客户联系人详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sy_ClientLinkManQuery GetClientLinkManByID(int id)
        {
            return linkManManage.GetSy_ClientLinkManQuery(id);
        }

        /// <summary>
        /// 据sql语句同参数添加或修改客户联系人
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public bool ClientLinkManEdit(string sqlStr, SqlParameter[] parms)
        {
            return db.ExecSqlTransaction(sqlStr, parms);
        }

        /// <summary>
        /// 删除客户联系人
        /// </summary>
        /// <returns></returns>
        public bool ClientLinkManDelete(int id)
        {
           return linkManManage.DeleteSy_ClientLinkMan(id);
        }

        public bool ClientLinkManUpdate(Sy_ClientLinkManQuery query)
        {
            return linkManManage.UpdateSy_ClientLinkMan(query);
        }

        public bool ClientLinkManInsert(Sy_ClientLinkManQuery query)
        {
            return linkManManage.InsertSy_ClientLinkMan(query);

        }

        public int GetTypeIDByTypeName(string name)
        {
            int id = 0;
            SqlParameter[] parms = new SqlParameter[3];
            parms[0] = new SqlParameter("@Name",name);
            parms[1] = new SqlParameter("@HKName", name);
            parms[2] = new SqlParameter("@ENGName", name);

            DataTable dt = codeManage.GetSy_CodeList(" Name=@Name or HKName=@HKName or ENGName=@ENGName", parms);
            if (dt != null && dt.Rows.Count > 0)
            {
                id = int.Parse(dt.Rows[0]["PKID"].ToString());
            }
            return id;            
        }

        public string GetTypeNameByTypeID(int typeID)
        {
            string name = string.Empty;
            Sy_CodeQuery query = codeManage.GetSy_CodeQuery(typeID);
            if (query != null)
            {
                name = query.ENGName;
            }
            return name;

        }

        public DataTable GteLinkMansByClientID(int clientID)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@ClientID",clientID.ToString());

            return linkManManage.GetSy_ClientLinkManList(" ClientID=@ClientID",parms);
        }
    }
}
